<?php
//mengubah dari array query menjadi HTML
include_once ('utility.php');

function convertQuery($query,$viewer_id)
{
	$count=0;
	$p_model = new Post_Model;
	while($q = mysql_fetch_array($query,MYSQL_BOTH))
	{
		/* print_r($q);  */
		$content_temp = explode("\n",$q["content"]);
		$relation_query = "SELECT type
							FROM post_tag 
							INNER JOIN post 
							ON post_tag.post_id =post.ID
							INNER JOIN tag
							ON tag.ID=post_tag.tag_id
							WHERE post.ID = $q[0]";
							
		$dbtag = mysql_query($relation_query) or die(mysql_error()); 
		/* CARI SELISIH WAKTU */
		$post_datetime = new DateTime($q['datetime'], new DateTimeZone('Asia/Jakarta'));	
		$now = new DateTime(null, new DateTimeZone('Asia/Jakarta'));	
		$post_diff_datetime_string = Utility::getDeltaTimeString($post_datetime, $now);

		echo "<div class=contentbox>";
		echo "<div class=list-image>";
		echo "<a href=content.php?id=$q[0]>"; //menuju ka halaman konten
		
		if(strcmp($q['post_type'],"image")==0)
		{
			echo "<img src=\"upload/".$content_temp[0]."\" alt=kontent width=\"300\" height=\"168\" ".$q["ID"]." />";
		}
		else if(strcmp($q['post_type'],"link")==0)
		{
			echo "<img src=konten1.png alt=dummy />";
		}
		else//untuk video
		{
			$tube_id = substr($content_temp[0],31, 11);	
			echo "<iframe width=300 height=233 src=http://www.youtube.com/embed/$tube_id frameborder=0 allowfullscreen></iframe>";
		}
		
		echo "</a>";
		echo "<span class=post_tag>Tags: ";
		$p_model->printRelevantTags($q[0]);
		echo "</span>";
		echo "</div>";//tutup div untuk list-image
		echo "<div class=list-description>";
			echo "<div class=\"right-align\">";
				echo "<a onclick=doLikeOp(\"like\@".$q[0]."\",\"id\")><img src=like.png width=20 height=20 alt=like></a>";
				echo "<a onclick=doLikeOp(\"dislike\@".$q[0]."\",\"id\")><img src=dislike.png width=20 height=20 alt=dislike></a>";
				$p_model->printLike($q["like_count"],$viewer_id,$q[0]);
				/* echo "<small><i id=\"content-like-$q[0]\">".$q["like_count"]."  people like this</i></small>"; */
				echo "<br/>";
				echo "<small><i>".$q[4]." people commented this post</i></small>";
				/* print_r ($q); */
			 echo "</div>";//tutup div untuk right-align
			echo "<h3><a href=content.php?id=$q[0]>$q[title]</a></h3>";
			echo "<p>$q[content]</p>";
			echo "<p><small><i>Uploaded by $q[username] $post_diff_datetime_string </i></small></p>";
		echo "</div>";//tutup div untuk list-description
		echo "</div>";//tutup div untuk contentbox
		$count++;//hitung jumlah baris array yang di fetch
	}
	return $count;
}	

function printPages($rownumber,$maxinpage)
{
	$npage = ceil(($rownumber/$maxinpage)*1.0);
	$temp=ceil($rownumber/$maxinpage);
	//echo $n_page;
	$temppage= "<a href=\"list-content.php?last=0\" class=\"navigation\">1</a>";
	$i=2;
	
	while($i<=$temp)
	{
		/* echo ( ceil(($rownumber/$maxinpage)*1.0)); */
		$temppage=$temppage."|<a href=\"list-content.php?last=".($i-1)*$maxinpage ."\"class=\"navigation\">".$i."</a>";
		$i++;
	}
	
	echo $temppage;
}


?>